package com.asdan.seededu.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


import com.asdan.seededu.entity.security.Signin;
import com.asdan.seededu.service.SigninService;
import com.asdan.seededu.util.DatabaseAccess;

public class SigninServiceImpl extends UtilImpl implements SigninService{
	public Signin getIdByEmailAndPassword(String email ,String password,int uid)
	{
		
		DatabaseAccess databaseAccess = new DatabaseAccess(false);
		Connection cn = null;
		PreparedStatement pstmt=null;
		ResultSet rs = null;
		Signin sch = null ;
		String sql0 = "select uid from user where email = ?";
		System.out.println(sql0);
		try{
			cn = databaseAccess.getConn();
			pstmt = cn.prepareStatement(sql0);
			pstmt.setString(1,email);
			rs = pstmt.executeQuery();
			System.out.println("rs:"+rs) ;
			if(rs.next())
			 {
				String sql = "select uid,name from user where email=? and password=? AND isok=1 ";
				System.out.println(sql);
				try
				{
					cn = databaseAccess.getConn();
					pstmt = cn.prepareStatement(sql);
					pstmt.setString(1,email);
					pstmt.setString(2,password ) ;
					rs = pstmt.executeQuery();
					if(rs.next())
					{
						sch= new Signin();
						sch.setUid(rs.getInt("uid")) ;
						sch.setName(rs.getString("name"));
						sch.setEmail(email);
						sch.setPassword("");
					}
					else{
						sch= new Signin();
						sch.setUid(-2);//密码正确
						sch.setName("");
						sch.setPassword("");
					}
				}catch(Exception e){
					e.printStackTrace();
					System.out.println(e.getMessage());
					}
			}
			else{
				sch= new Signin();
				sch.setUid(-1);//用户名不存在
				sch.setName("");
				sch.setPassword("");
			}
		}catch(Exception e){
				e.printStackTrace();
				System.out.println(e.getMessage());
				}finally{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
						try{
							pstmt.close();
							cn.close();
							databaseAccess.close() ;
							}catch(Exception ex){
								pstmt = null ;
								cn = null ;
								}
		}
		
		return sch ;
	}
}
